/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package email.api;

/**
 *
 * @author Puma
 */
import java.net.URI;
import java.net.URISyntaxException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.*;
import java.util.List;

public class Postgresql {

    private Statement stmt = null;

    public Connection Connect() {
        Connection c = null;
        try {
            Class.forName("org.postgresql.Driver");
            c = DriverManager
                    .getConnection("jdbc:postgresql://localhost:5432/jrockefeller_development",
                            //.getConnection("jdbc:postgresql://localhost:5432/jrockefeller_test",
                            "jrockefeller", "jrockefeller");
            
            
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println(e.getClass().getName() + ": " + e.getMessage());
            System.exit(0);
        }

        return c;
    }

    public void insert(String sql) {

        try {
            Connection c = Connect();
            stmt = c.createStatement();

            stmt.executeUpdate(sql);

            stmt.close();
            c.close();

        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
    }

    public void update(String sql) {

        try {
            Connection c = Connect();
            stmt = c.createStatement();

            stmt.executeUpdate(sql);

            stmt.close();
            c.close();

        } catch (SQLException e) {
            System.out.println(e.getMessage());
        }
    }

    public List<Deal> select_deal(String sql) {
        List<Deal> list_deal = null;
        try {
            Connection c = Connect();
            stmt = c.createStatement();
            ResultSet rs = stmt.executeQuery(sql);
            while (rs.next()) {

                //Deal deal = new Deal(rs.getString("Imagen"), rs.getString("Url"), rs.getString("Texto"));
                // list_deal.add(deal);
            }
            rs.close();
            stmt.close();
            c.close();

        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
        return list_deal;
    }

    public void delete(String sql) {
        try {
            Connection c = Connect();
            stmt = c.createStatement();
            stmt.executeUpdate(sql);

            stmt.close();
            c.close();
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}
